package ar.Location.Api.Wikipedia;

import android.app.Activity;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.View;
import ar.Location.Helper.BitmapProvider;
import ar.Location.Helper.CalculationProvider;
import ar.Location.Helper.StringHelper;
import ar.Location.PlaceObject.Place;
import ar.Location.UI.PlaceResultItemView;
import ar.activity.PlaceDetailActivity;
import ar.activity.R;
import ar.activity.R.drawable;
import ar.globals.Globals;

public class WikipediaPlace extends Place {
	
	protected static final String TAG = "WikipediaPlace";

	public WikipediaPlace(String dfName) {
		categoryName = dfName;
		websiteUrl = "http://en.wikipedia.org/wiki/Main_Page";
	}

	@Override
	public void bindDataToViewDetail(final PlaceDetailActivity detailView) {
		// TODO Auto-generated method stub
		if(detailView != null) {
			detailView.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					detailView.imgViewIcon.setImageBitmap(BitmapProvider.bmpIconWikipedia);
					
					detailView.txtViewName.setText(name);
					detailView.txtViewDescription.setText(Html.fromHtml(description));
					
					if(imageUrl == null || imageUrl.length() == 0) {
						detailView.imgViewThumb.setImageResource(R.drawable.icon_wikipedia_sphere);
						detailView.imgViewThumb.setVisibility(View.VISIBLE);
						detailView.progressBarUpdateThumbImage.setVisibility(View.GONE);
					}
					
					if(latitude.length() > 0 && longitude.length() > 0) {
						float myLat = Float.parseFloat(Globals.LAT);
						float myLng = Float.parseFloat(Globals.LNG);
						float lat = Float.parseFloat(latitude);
						float lng = Float.parseFloat(longitude);
						float dist = CalculationProvider.distFrom(myLat, myLng, lat, lng);
						String strDistance = StringHelper.getStringDistance(Math.round(dist));
						detailView.txtViewDistance.setText(String.valueOf(strDistance));
					}
					else {
						detailView.txtViewDistance.setText("");
					}
					
					if(address != null & address.length() > 0) {
						detailView.txtViewAddress.setText("Address: " + address);
					} else {
						detailView.txtViewAddress.setVisibility(View.GONE);
						detailView.imgViewBulletAddress.setVisibility(View.GONE);
					}
					
					if(websiteUrl != null && websiteUrl.length() > 0) {
						detailView.txtViewWebsiteUrl.setText("Website: " + websiteUrl);
					} else {
						detailView.txtViewWebsiteUrl.setVisibility(View.GONE);
						detailView.imgViewBulletWebsite.setVisibility(View.GONE);
					}
					
					if(dateTime != null && dateTime.length() > 0) {
						detailView.txtViewDateTime.setText(dateTime);
					} else {
						detailView.txtViewDateTime.setVisibility(View.GONE);
						detailView.imgViewBulletDateTime.setVisibility(View.GONE);
					}
					
					detailView.txtViewPhoneNumber.setVisibility(View.GONE);
					detailView.imgViewBulletPhoneNumber.setVisibility(View.GONE);
					detailView.actionCall.setVisibility(View.GONE);
				}
			});
		}
	}

	@Override
	public void bindDataToListViewItem(final PlaceResultItemView listViewItem) {
		// TODO Auto-generated method stub
		if(listViewItem != null) {
			((Activity)listViewItem.listViewItemContext).runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					listViewItem.imgIconName.setImageBitmap(BitmapProvider.bmpIconWikipedia);
					listViewItem.imgIconDescription.setImageResource(R.drawable.icon_bullet);
					listViewItem.txtViewName.setText(name);
					listViewItem.txtViewDescription.setText(Html.fromHtml(StringHelper.getTruncateString(description)));
					
					if(latitude.length() > 0 && longitude.length() > 0) {
						float myLat = Float.parseFloat(Globals.LAT);
						float myLng = Float.parseFloat(Globals.LNG);
						float lat = Float.parseFloat(latitude);
						float lng = Float.parseFloat(longitude);
						float dist = CalculationProvider.distFrom(myLat, myLng, lat, lng);
						String strDistance = StringHelper.getStringDistance(Math.round(dist));
						listViewItem.txtViewDistance.setText(String.valueOf(strDistance));
					}
					else {
						listViewItem.txtViewDistance.setText("");
					}
				}
			});
		}
	}

	@Override
	public void updateImage(final PlaceResultItemView listViewItem, final Bitmap bmp) {
		// TODO Auto-generated method stub
		if(listViewItem != null) {
			((Activity)listViewItem.listViewItemContext).runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					listViewItem.imgView.setVisibility(View.VISIBLE);
					listViewItem.progressBar.setVisibility(View.GONE);
					if(bmp != null) {
						listViewItem.imgView.setImageBitmap(bmp);
					} else {
						listViewItem.imgView.setImageResource(drawable.icon_wikipedia_sphere);
					}
				}
			});
		}
		
	}
}
